<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\response;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    public function __construct(){
        date_default_timezone_set('Asia/Shanghai');
    }
    protected $statusCodes = [
        'get_ok' => ['code' => 200, 'msg' => '获取数据成功'],
        'get_error' => ['code' => 404, 'msg' => '未发现该对象'],
        'add_ok' => ['code' => 200, 'msg' => '新增数据成功'],
        'add_error' => ['code' => 500, 'msg' => '新增数据失败'],
        'update_ok' => ['code' => 200, 'msg' => '更新数据成功'],
        'update_error' => ['code' => 500, 'msg' => '更新数据失败'],
        'delete_ok' => ['code' => 200, 'msg' => '删除数据成功'],
        'delete_error' => ['code' => 500, 'msg' => '删除数据失败'],
        'empty_error' => ['code' => 500, 'msg' => '参数为空或无效'],
    ];
    public function page(Request $request)
    {
        $page = $request->input('page') ? $request->input('page') : 1;
        $pageNum = $request->input('pageNum') ? $request->input('pageNum') : 10;
        $skip = ($page - 1) * $pageNum;
        $take = $pageNum;
        return ['skip' => $skip, 'take' => $take];
    }

    public function respond($status, $data = [])
    {   
        $status = $this->statusCodes[$status];
        if (!empty($data)) {
            $status['data'] = $data;
        }
        return response()->json($status);
    }
}
